from qiniu import Auth, put_file
from flask import current_app


class QiniuTool():
    def __init__(self):
        ak = current_app.config.get('QINIU_AK')
        sk = current_app.config.get('QINIU_SK')

        self.q = Auth(ak, sk)
        self.bucket_name = current_app.config.get('QINIU_BUCHET_NAME')

    def upload(self, localfilepath, newfilename):

        token = self.q.upload_token(self.bucket_name, newfilename, 3600)
        ser = put_file(token, newfilename, localfilepath, version='v2')
        print(ser)
        if ser[0]['key'] == newfilename:
            return newfilename
        return False



if __name__ == '__main__':
    from app import app
    with app.app_context():
        QiniuTool().upload('./../static/1.jpg', 'static/img/1.jpg')